package com.whq.knowverywell.adapter;

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.whq.knowverywell.R;
import com.whq.knowverywell.activity.ManagerAccountActivity;
import com.whq.knowverywell.business.model.User;
import com.whq.knowverywell.util.LogUtil;

public class ManagerAccountAdapter extends BaseAdapter {
	private final static String TAG = ManagerAccountAdapter.class.getSimpleName();
	
	private ManagerAccountActivity activity = null;
	private LayoutInflater inflater = null;
	private List<User> userList = new ArrayList<User>(0);
	
	public ManagerAccountAdapter(ManagerAccountActivity activity, List<User> userList) {
		this.activity = activity;
		this.inflater = LayoutInflater.from(this.activity);
		if (userList != null) {
			this.userList = userList;
		}
	}
	
	@Override
	public int getCount() {
		return this.userList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.userList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LogUtil.d(TAG, "get view position : " + position);

		ViewHolder holder = null;
		if (convertView == null) {
			convertView = this.inflater.inflate(R.layout.manager_account_item, null);
			holder = new ViewHolder();
			holder.layoutAccountDetail = (RelativeLayout) convertView.findViewById(R.id.layout_account_detail);
			holder.tvAccountName = (TextView) convertView.findViewById(R.id.tv_account_name);
			holder.ivDeleteAccount = (ImageView) convertView.findViewById(R.id.iv_delete_account);
			holder.ivDivider = (ImageView) convertView.findViewById(R.id.iv_divider);
			holder.layoutAddAccount = (RelativeLayout) convertView.findViewById(R.id.layout_add_account);
			holder.ivAddAccount = (ImageView) convertView.findViewById(R.id.iv_add_account);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final User oneRecord = userList.get(position);
		if (oneRecord == null) {
			return convertView;
		}
		LogUtil.i(TAG, "record[id: " + oneRecord.getUserId() + "],[id: " + oneRecord.getUserName() + "]");
		
		holder.layoutAccountDetail.setVisibility(View.GONE);
		holder.layoutAddAccount.setVisibility(View.GONE);
		holder.ivDivider.setVisibility(View.VISIBLE);
		if (oneRecord.isShowAddAccountBtn()) {
			holder.layoutAddAccount.setVisibility(View.VISIBLE);
			holder.ivDivider.setVisibility(View.GONE);
			holder.ivAddAccount.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					activity.go2AddAccount();
				}
			});
		} else {
			holder.layoutAccountDetail.setVisibility(View.VISIBLE);
			holder.tvAccountName.setText(oneRecord.getUserName());
			holder.ivDeleteAccount.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					activity.deleteOneAccount(oneRecord.getUserId(), oneRecord.getGroupCode(), oneRecord.getUserName());
				}
			});
		}
		
		return convertView;
	}
	
	class ViewHolder {
		RelativeLayout 	layoutAccountDetail;
		TextView 		tvAccountName;
		ImageView 		ivDeleteAccount;
		ImageView		ivDivider;
		
		RelativeLayout 	layoutAddAccount;
		ImageView 		ivAddAccount;
		
	}
}
